
package org.itboys.mobile.util.getui;


import com.gexin.rp.sdk.base.impl.Target;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.mobile.entity.mongo.SundayCid;
import org.itboys.mobile.util.getui.pojo.BaseBean;
import org.itboys.mobile.util.getui.utils.SendMessageUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * 作者：jiangxiong 日期：2016年4月20日 联系方式 ：empty90911@126.com 描述：天街推送消息
 */

public class GeTuiUtil {

	
	// appId
	public static String APPID = "bUUDAdxB0U8LXxlROePDz6";
	// appKey
	public static String APPKEY = "oWI6OYv6Ia8HhszOw7Zhv";
	// masterSecret
	public static String MASTERSECRET = "qKJUmspeR4A2vTwGP8meX1";
	// host
	public static String HOST = "http://sdk.open.api.igexin.com/apiex.htm";
	/*
	 * 推送消息
	 * @param sundayCid
	 * @param message
	 * @param code 推送标识，指定app打开哪一个页面，项目自定义：1，管理版-配送。
	 * @param param 推送额外参数
	 * @return
	 */

	public static void sendMessage(SundayCid sundayCid,
			String message, Integer code,Long param) {

		//String payload = "{\"code\":" + code + ",\"param\":" + param  +",\"message\":" + message+ "}";
		//安卓payload
		String androidPayload= "{\"code\":\"" +code +
				  "\",\"param\":\"" + param +
				  "\",\"message\":\"" + message +"\"}";
		//苹果payload
		String iosPayload = "{\"code\":\"" +code +
				  "\",\"param\":\"" + param +"\"}";
		// baseBean.setContent("{\"value\":\"aa\",\"key\":\"90000\",\"info\":\"info\"}");

		BaseBean baseBean = new BaseBean(APPID, APPKEY, MASTERSECRET, HOST);
		//baseBean.setTitle("我是title");

	//	baseBean.setContent(payload);
	//	baseBean.setPayload(payload);
		//苹果端
		if (sundayCid.getType().equalsIgnoreCase("ios")) {
			//baseBean.setContent("123");
			baseBean.setPayload(iosPayload);
			baseBean.setMessage(message);
		}
		//安卓
		if (sundayCid.getType().equalsIgnoreCase("android")) {
			baseBean.setContent(androidPayload);
		}
		baseBean.setType(2);
		baseBean.setActionLocKey("");
		baseBean.setBadge(1);
		// baseBean.setco
		//加入推送语音

	/*if (code == Finals.GETUI_CODE_DFH) {
			baseBean.setSound("order.wav");
		}else if(code ==Finals.GETUI_CODE_PSZ_DJD){
			baseBean.setSound("order2.wav");
		}else if(code ==Finals.GETUI_CODE_DFH_HOMEMAKING){
			baseBean.setSound("order3.wav");
		}else if(code ==Finals.GETUI_CODE_PSZ_DJD_HOMEMAKING){
			baseBean.setSound("order4.wav");
		}*/

		List<Target> androidTargets = new ArrayList<Target>();
		List<Target> iosTargets = new ArrayList<Target>();


			Target target = new Target();
			target.setAppId(APPID);
			target.setClientId(sundayCid.getCid());
			// 苹果
			if (sundayCid.getType().equalsIgnoreCase("ios")) {
				System.out.println("--------苹果客户端----------");
				iosTargets.add(target);
			}
		// 安卓
			if (sundayCid.getType().equalsIgnoreCase("android")){
				System.out.println("-----------安卓客户端-------");
				androidTargets.add(target);

			}
		//}
		try {
			androidTargets = androidTargets.size() > 0 ? androidTargets : null;
			iosTargets = iosTargets.size() > 0 ? iosTargets : null;
			// 不要全部推送
			if (ListUtil.isNull(androidTargets) && ListUtil.isNull(iosTargets)){
				return ;
			}

			// System.out.println("---------------------");
			SendMessageUtil.sendMsg(baseBean, androidTargets, iosTargets);
			//sendMsg(baseBean, androidTargets, iosTargets);
			//推送成功后记录推送的数量
			//TokenRecordService.
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		//安卓
		String atoken="31e45b873df3f854a1ff2e039dfdf3a0";
		//苹果
		String itoken="4b7a8868a9c3ea0b1a08d8e710f4020a";
		String message="农厨,测试推送，打开配送";
		Integer code=1;
		SundayCid aCid=new SundayCid();
		aCid.setType("android");
		aCid.setCid(atoken);
		sendMessage(aCid,
				 message,  code, null);
		SundayCid iCid=new SundayCid();
		iCid.setType("ios");
		iCid.setCid(itoken);
		sendMessage(iCid,
				message,  code, null);
		
	}
}
